package com.fishbone.file.obs;

import com.fishbone.file.config.ObsProperties;
import com.obs.services.ObsClient;
import com.obs.services.exception.ObsException;
import com.obs.services.model.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.InputStream;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * @ClassName FishBoneObsClient
 * @Author huyaxi
 * @DATE 2020/4/20 16:36
 */
public class HuaWeiObsClient {

    private ObsClient obsClient;


    public HuaWeiObsClient(ObsProperties properties) {
        this.obsClient =  new com.obs.services.ObsClient(properties.getAk(),properties.getSk(),properties.getEndpoint());
    }

    /**
     * 文件模式上传文件
     * @param bucket
     * @param objectKey
     * @param file
     * @return
     */
    public PutObjectResult upload(String bucket, String objectKey, File file) {
        return obsClient.putObject(bucket, objectKey, file);
    }


    /**
     * 流模式文件上传
     * @param bucket
     * @param objectKey
     * @param stream
     * @return
     */
    public PutObjectResult upload(String bucket, String objectKey, InputStream stream) {
        return obsClient.putObject(bucket, objectKey, stream);
    }


    /**
     * 获取obs信息
     * @param bucket
     * @param objectKey
     * @return
     */
    public ObsObject getObsObject(String bucket, String objectKey){
        return obsClient.getObject(bucket, objectKey);
    }


    /**
     * 获取obs对象流
     * @param bucket
     * @param objectKey
     * @return
     */
    public InputStream getObsInputStream(String bucket, String objectKey) {
        return obsClient.getObject(bucket,objectKey).getObjectContent();
    }


    /**
     * 创建OBS桶
     * @param bucketName
     * @return
     */
    public ObsBucket createBucket(String bucketName) {
        return obsClient.createBucket(bucketName);
    }


    /**
     * 文件分片上传
     * 数据初始化
     * @param bucketName
     * @return
     */
    public String claimUploadId(String bucketName,String objectKey) throws Exception{
        InitiateMultipartUploadRequest request = new InitiateMultipartUploadRequest(bucketName, objectKey);
        InitiateMultipartUploadResult result = obsClient.initiateMultipartUpload(request);
        return result.getUploadId();
    }


    /**
     * 上传分段
     * @param multipartFile
     * @param bucketName
     * @param objectKey
     * @param partNum
     * @param uploadId
     * @return
     * @throws Exception
     */
    public PartEtag uploadPart(MultipartFile multipartFile, String bucketName, String objectKey, Integer partNum, String uploadId) throws Exception{
        UploadPartResult uploadPartResult = obsClient.uploadPart(bucketName,objectKey,uploadId,partNum+1,multipartFile.getInputStream());
        return new PartEtag(uploadPartResult.getEtag(), uploadPartResult.getPartNumber());
    }


    /**
     * 合并分段
     * @param uploadId
     * @param bucketName
     * @param objectKey
     */
    public CompleteMultipartUploadResult completeMultipartUpload(String uploadId, String bucketName, String objectKey, List<PartEtag> partETags) {
        listAllParts(bucketName, objectKey, uploadId);
        Collections.sort(partETags, Comparator.comparingInt(PartEtag::getPartNumber));
        CompleteMultipartUploadRequest completeMultipartUploadRequest =
                new CompleteMultipartUploadRequest(bucketName, objectKey, uploadId, partETags);
        return obsClient.completeMultipartUpload(completeMultipartUploadRequest);
    }

    private void listAllParts(String bucketName,String objectKey,String uploadId) throws ObsException {
        ListPartsRequest listPartsRequest = new ListPartsRequest(bucketName, objectKey, uploadId);
        obsClient.listParts(listPartsRequest);
    }
}
